package com.jetinno.simple;

import android.os.Handler;
import android.os.Looper;

import com.jetinno.utils.LoadMvpView;
import com.jetinno.utils.LogUtils;
import com.jetinno.utils.SimpleCallback;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by zhangyuncai on 2021/7/29.
 */
public class SimplePresenter implements LoadMvpView {
    protected final LoadMvpView loadMvpView;
    protected String TAG = this.getClass().getSimpleName();
    protected Handler mainHandler = new Handler(Looper.getMainLooper());
    protected CompositeDisposable mCompositeDisposable = new CompositeDisposable();


    public SimplePresenter(LoadMvpView loadMvpView) {
        this.loadMvpView = loadMvpView;
    }

    protected void runOnUiThread(Runnable runnable) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            runnable.run();
        } else {
            mainHandler.post(runnable);
        }
    }

    protected void delay(long delay, SimpleCallback<Object> callback) {
        Disposable subscribe = Observable.timer(delay, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> {
                    callback.callback(true);
                }, throwable -> {
                    LogUtils.logException(TAG, throwable);
                });
        mCompositeDisposable.add(subscribe);
    }


    /**
     * 最终走向
     *
     * @param observable
     * @param resultConsumer
     * @param <B>
     */
    public <B> void baseSubscribe(Observable<B> observable, Consumer<B> resultConsumer, final Consumer<Throwable> throwableConsumer) {
        if (resultConsumer == null) {
            throw new NullPointerException("resultConsumer==null!!");
        }
        Disposable subscribe = observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {
                    if (loadMvpView != null) {
                        loadMvpView.showLoadingBar("");
                    }
                })
                .doFinally(() -> {
                    if (loadMvpView != null) {
                        loadMvpView.dismissLoadingBar();
                    }
                })
                .subscribe(b -> {
                    resultConsumer.accept(b);
                }, throwable -> {
                    LogUtils.logException(TAG, throwable);
                    if (throwableConsumer != null) {
                        throwableConsumer.accept(throwable);
                    }
                });
        mCompositeDisposable.add(subscribe);
    }


    public void onDestory() {
        mCompositeDisposable.dispose();
    }


    @Override
    public void showLoadingBar(String msg) {
        if (loadMvpView != null) {
            loadMvpView.showLoadingBar(msg);
        }
    }

    @Override
    public void dismissLoadingBar() {
        if (loadMvpView != null) {
            loadMvpView.dismissLoadingBar();
        }
    }
}
